package com.googlecode.studentrecords;


/**Interacts with FileIO to edit and store student information*/
public class SRDatabase{
	
	/**The student database*/
	String[][] students = new String[5][5];
	/**The file interface*/
	FileIO file = new FileIO();
	
	/**Populates array of students*/
	public SRDatabase()
	{
		for(int row = 1; row <= students.length; row ++)
		{
			String path = "Student" + row + ".txt";
			//System.out.println(path);
			students[row - 1] = file.readFile(path);
		}	
	}
	
	/**Prints all students*/
	public void printStudents()
	{
		for(int i = 0; i < students.length; i ++)
		{
			for(String info : students[i])
			{
				System.out.println(info);
			}
			System.out.println("");
		}
		
	}
	
	/**Prints student with given ID
	 * 
	 * @param ID		Student identification number
	 */
	public void printStudent(String ID)
	{
		try
		{
		for(String info : students[findStudent(ID)])
			System.out.println(info);
		System.out.println("");
		}
		catch(ArrayIndexOutOfBoundsException e)
		{
			System.out.println("No student found with the given ID.");
		}
	}
	
	/**Replaces student with the given oldID's
	 * ID with newID
	 * 
	 * @param oldID			ID to be replaced
	 * @param newID			ID to replace with
	 */
	public void updateID(String oldID, String newID)
	{
		/**Indicates whether or not newID is an integer*/
		boolean isInt = isInt(newID);
		
		if(newID.length() == 6 && isInt && findStudent(newID) == -1)
		{
			try
			{
				students[findStudent(oldID)][0] = newID;
				file.editFile("Student" + (findStudent(newID) + 1) +".txt", oldID, newID);				
			}
			catch(ArrayIndexOutOfBoundsException e)
			{
				System.out.println("No student found with the given ID.");
			}
			
		}
		else if(!isInt || newID.length() != 6)
			System.out.println("The new ID given is not a valid ID.\n"
					+ "An ID must contain six numbers between 1 and 10.");
		else if(findStudent(newID) != -1)
			System.out.println("A student already exists with the given ID.");
	}
	
	/**Replaces student name with given name
	 * 
	 * @param ID			Student identification number
	 * @param newName		Name to be replaced with
	 */
	public void updateName(String ID, String newName)
	{
		try
		{
			String temp = students[findStudent(ID)][1];
			students[findStudent(ID)][1] = newName;
			file.editFile("Student" + (findStudent(ID) + 1) + ".txt", temp, newName);
		}
		catch(ArrayIndexOutOfBoundsException e)
		{
			System.out.println("No student found with the given ID.");
		}
	}
	
	/**Replaces student address with given address
	 * 
	 * @param ID			Student identification number
	 * @param newAddress	Address to be replaced with
	 */
	public void updateAddress(String ID, String newAddress)
	{
		try
		{
			String temp = students[findStudent(ID)][2];
			students[findStudent(ID)][2] = newAddress;
			file.editFile("Student" + (findStudent(ID) + 1) + ".txt", temp, newAddress);
		}
		catch(ArrayIndexOutOfBoundsException e)
		{
			System.out.println("No student found with the given ID.");
		}
	}
	
	/**Replaces student emergency contact number 
	 * with given number
	 * 
	 * @param ID			Student identification number
	 * @param newNumber		Emergency contact number to be replaced with
	 */
	public void updateEmergencyContact(String ID, String newNumber)
	{
		if(isInt(newNumber) && newNumber.length() == 10)
		{
			try
			{
				String temp = students[findStudent(ID)][3];
				students[findStudent(ID)][3] = newNumber;
				file.editFile("Student" + (findStudent(ID) + 1) + ".txt", temp, newNumber);
			}
			catch(ArrayIndexOutOfBoundsException e)
			{
				System.out.println("No student found with the given ID.");
			}
		}
		else
			System.out.println("Please enter a valid phone number.");
	}
	
	/**Replaces student password with given password
	 * 
	 * @param ID			Student identification number
	 * @param newPassword	Password to be replaced with
	 */
	public void updatePassword(String ID, String newPassword)
	{
		if(isInt(newPassword) && newPassword.length() < 10)
		{
			try
			{
				String temp = students[findStudent(ID)][4];
				students[findStudent(ID)][4] = newPassword;
				file.editFile("Student" + (findStudent(ID) + 1) + ".txt", temp, newPassword);
			}
			catch(ArrayIndexOutOfBoundsException e)
			{
				System.out.println("No student found with the given ID.");
			}
		}
		else
			System.out.println("Please enter a password that's less than 10 characters long.");
	}
	
	/**Finds and returns the index of the student
	 * with the given ID
	 * 
	 * @param ID			Student identification number
	 * @return				Index of the student queried
	 */
	public int findStudent(String ID)
	{
		try
		{
			for(int i = 0; i < students.length; i ++)
			{
				
				if(students[i][0].equals(ID))
				{
					return i;
				}
			}
			return -1;
		}
		catch(NullPointerException e)
		{
			return -1;
		}
	}
	
	/**Checks if the given String is an Integer
	 * 
	 * @param string		String to be tested
	 * @return				Returns true if string is an Integer
	 */
	public boolean isInt(String string)
	{
		try { 
	        Integer.parseInt(string);
	        return true;
	    } catch(NumberFormatException e) { 
	    	return false;
	    }
	}
	
	/**Gets the name of a student based on ID number
	 * 
	 * @param ID		String ID to be tested
	 * @return			Returns name of student
	 */
	public String getName(String ID)
	{
		String name;
		name = students[findStudent(ID)][1];
		return name;
	}
	
	/**Gets the address of a student based on ID number
	 * 
	 * @param ID		ID number to be tested
	 * @return			Returns address of corresponding student
	 */
	public String getAddress(String ID)
	{
		String address;
		address = students[findStudent(ID)][2];
		return address;
	}
	
	/**Gets the emergency contact of a student based
	 * on ID number
	 * 
	 * @param ID		ID number to be tested
	 * @return			Returns emergency contact of corresponding
	 * 					student
	 */
	public String getEmergencyContact(String ID)
	{
		String emergencyContact;
		emergencyContact = students[findStudent(ID)][3];
		return emergencyContact;
	}
	
	/**Gets all information of a student based on
	 * ID number
	 * 
	 * @param ID		ID number to be tested
	 * @return			Returns array of information of
	 * 					corresponding student
	 */
	public String[] getStudent(String ID)
	{
		String test = "";
		try
		{
		for(String info : students[findStudent(ID)])
			test += info;
		}
		catch(ArrayIndexOutOfBoundsException e)
		{
			System.out.println("No student found with the given ID.");
		}
		
		
		return students[findStudent(ID)];
		
	}
	
	/**Gets a print-ready string of student information
	 * based on ID number
	 * 
	 * @param ID		ID number to be tested
	 * @return			Returns formatted String of 
	 * 					information of corresponding student
	 */
	public String getFormattedRecord(String ID)
	{
		String[] studentRecord = new String[5];
		studentRecord = getStudent(ID);
		String formattedRecord = "";

		formattedRecord = "Student Info"
					+ "\n--------------------------\n"
					+ "ID: " + studentRecord[0]
					+ "\nName: " + studentRecord[1]
					+ "\nAddress: " + studentRecord[2]
					+ "\nEmergency Contact No.: " + studentRecord[3]
					+ "\n";

		System.out.println(formattedRecord);
		return formattedRecord;
	}

}
